/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.hazard.supervise.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.hazard.supervise.dto.HazClassifySuperviseExcelDTO;
import com.hopes.bizz.hazard.supervise.entity.HazClassifySuperviseEntity;
import com.hopes.bizz.hazard.supervise.entity.HazClassifySuperviseRecordEntity;
import com.hopes.bizz.hazard.supervise.mapper.HazClassifySuperviseMapper;
import com.hopes.bizz.hazard.supervise.service.HazClassifySuperviseRecordService;
import com.hopes.bizz.hazard.supervise.service.HazClassifySuperviseService;
import com.hopes.bizz.hazard.supervise.vo.HazClassifySuperviseExcelVO;
import com.hopes.bizz.hazard.supervise.vo.HazClassifySuperviseVO;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * 企业分类监管
 *
 * @author daijie
 * @date 2024-01-29 14:42:21
 */
@Service
public class HazClassifySuperviseServiceImpl extends ServiceImpl<HazClassifySuperviseMapper, HazClassifySuperviseEntity> implements HazClassifySuperviseService {

    @Autowired
    private HazClassifySuperviseRecordService hazClassifySuperviseRecordService;

    @Override
    public ValidateImportBO<HazClassifySuperviseExcelDTO> validateImport(List<HazClassifySuperviseExcelDTO> excelDTOList) {
        ValidateImportBO<HazClassifySuperviseExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<HazClassifySuperviseExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public HazClassifySuperviseEntity importDtoToEntity(HazClassifySuperviseExcelDTO excelDto) {
        HazClassifySuperviseEntity entity= new HazClassifySuperviseEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public HazClassifySuperviseExcelVO exportEntityToVo(HazClassifySuperviseEntity entity) {
        HazClassifySuperviseExcelVO excelVO = new HazClassifySuperviseExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

    @Override
    public Page page(Page page, HazClassifySuperviseEntity hazClassifySupervise) {
        LambdaQueryWrapper<HazClassifySuperviseEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(hazClassifySupervise.getRiskLevel()), HazClassifySuperviseEntity::getRiskLevel, hazClassifySupervise.getRiskLevel());
        wrapper.eq(StrUtil.isNotBlank(hazClassifySupervise.getSuperviseType()), HazClassifySuperviseEntity::getSuperviseType, hazClassifySupervise.getSuperviseType());
        wrapper.eq(ObjUtil.isNotNull(hazClassifySupervise.getOrgId()), HazClassifySuperviseEntity::getOrgId, hazClassifySupervise.getOrgId());

        Page result = this.page(page, wrapper);
        List<HazClassifySuperviseEntity> classifySuperviseList = result.getRecords();
        List<HazClassifySuperviseVO> voList = new ArrayList<>();
        for (HazClassifySuperviseEntity hazClassifySuperviseEntity : classifySuperviseList) {
            HazClassifySuperviseVO vo = new HazClassifySuperviseVO();

            BeanUtils.copyProperties(hazClassifySuperviseEntity, vo);
            LambdaQueryWrapper<HazClassifySuperviseRecordEntity> recordWrapper = Wrappers.lambdaQuery();
            recordWrapper.eq(HazClassifySuperviseRecordEntity::getClassifySuperviseId, hazClassifySuperviseEntity.getId())
                    .orderByDesc(HazClassifySuperviseRecordEntity::getCreateTime);
            HazClassifySuperviseRecordEntity recordEntity = hazClassifySuperviseRecordService.getOne(recordWrapper, false);

            vo.setScoreJson(recordEntity.getScoreJson());
            voList.add(vo);
        }

        result.setRecords(voList);
        return result;
    }
}